package com.simple.editor.ui;

import com.simple.editor.action.ActionManager;
import com.simple.editor.action.EditorUIAction;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * {@link FileTreePane} provides "Project view" panel.
 * <p/>
 * Created with IntelliJ IDEA.
 * User: vlad
 * Date: 6/2/13
 * Time: 2:31 PM
 * To change this template use File | Settings | File Templates.
 */
public class FileTreePane extends JPanel {

    private final ActionManager actionManager;
    private final JTree fileTree;
    private FileModel root = null;
    private FileSystemTreeModel fsTreeModel;


    public FileTreePane(final ActionManager actionManager) {
        this.actionManager = actionManager;
        setPreferredSize(new Dimension(250, 400));

        fsTreeModel = new FileSystemTreeModel(root);
        fileTree = new JTree(fsTreeModel);
        fileTree.addMouseListener(createMouseListener());
        final JScrollPane jScrollPane = new JScrollPane(fileTree,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jScrollPane.setBorder(BorderFactory.createEtchedBorder());

        setLayout(new GridLayout());
        add(jScrollPane, BorderLayout.CENTER);
    }

    private MouseListener createMouseListener() {
        return new MouseAdapter() {

            @Override
            public void mouseClicked(final MouseEvent e) {
                final int selRow = fileTree.getRowForLocation(e.getX(), e.getY());
                if (selRow != -1) {
                    final FileModel file = (FileModel) fileTree.getLastSelectedPathComponent();
                    if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2 && file != null && !file.isDirectory()) {
                        actionManager.runAction(EditorUIAction.Type.OPEN, file);
                    }
                }

            }

            @Override
            public void mouseReleased(final MouseEvent e) {
                final int selRow = fileTree.getRowForLocation(e.getX(), e.getY());
                if (selRow != -1 && SwingUtilities.isRightMouseButton(e)) {
                    final int row = fileTree.getClosestRowForLocation(e.getX(), e.getY());
                    fileTree.setSelectionRow(row);
                    final JPopupMenu menu = new JPopupMenu();
                    final FileModel file = (FileModel) fileTree.getLastSelectedPathComponent();

                    if (file != null && file.isFile()) {
                        menu.add(new JMenuItem(new AbstractAction("Open") {
                            @Override
                            public void actionPerformed(final ActionEvent e) {
                                actionManager.runAction(EditorUIAction.Type.OPEN, file);
                            }
                        }));

                        menu.add(new JMenuItem(new AbstractAction("Delete") {
                            @Override
                            public void actionPerformed(final ActionEvent e) {
                                actionManager.runAction(EditorUIAction.Type.DELETE, file);
                            }
                        }));

                        menu.show(e.getComponent(), e.getX(), e.getY());
                    }
                }

            }
        };
    }

    public void refreshTree() {

        fsTreeModel = new FileSystemTreeModel(root);
        fileTree.setModel(fsTreeModel);
        fileTree.revalidate();

    }

    public void open(final FileModel file) {
        root = file;
        refreshTree();
    }
}

